package com.pmsystem.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.VillageMapper;
import com.pmsystem.pojo.Village;
import com.pmsystem.pojo.VillageExample;
import com.pmsystem.pojo.VillageExample.Criteria;


/**
 * 
* <p>Title: VillageService</p>  
* <p>Description:处理小区信息的业务层 </p>  
* @author 树昌  
* @date 2019年1月18日
 */
@Service
public class VillageService {
	
	@Autowired
	VillageMapper villageMapper;
	
	/**
	 * 
	 * <p>Title: addVillage</p>  
	 * <p>Description:添加一条小区的信息 </p>  
	 * @author 树昌
	 * @param village
	 * @return 
	 */
	public boolean addVillage(Village village) {
		
		System.out.println(village.getVillageName());
		int row = villageMapper.insertSelective(village);
		return row==1?true:false;
	}
	
	
	
	/**
	 * 
	 * <p>Title: showVillage</p>  
	 * <p>Description:查询所有未被删除的所有小区信息 </p>  
	 * @author 树昌
	 * @return
	 */
	public List<Village> showVillage(String keyword, Integer startRow, Integer pageSize) {

		VillageExample villageExample = new VillageExample();
		Criteria criteria = villageExample.createCriteria();
		// 只有enable值为1的记录才可以被查询
		criteria.andEnableEqualTo((byte) 1);
		// 模糊查询
		if (keyword != null) {
			keyword = "%" + keyword + "%";
			criteria.andVillageNameLike(keyword);
		}
		// 分页查询
		if (startRow != null && pageSize != null) {
			if (((startRow - 1) * pageSize) >= 0 && pageSize >= 1) {
				villageExample.setStartRow((startRow - 1) * pageSize);
				villageExample.setPageSize(pageSize);
			} else {
				return null;
			}
		}
		// 没有条件则为全查询

		List<Village> listvillage = villageMapper.selectByExample(villageExample);
		for (Village village : listvillage) {
			System.out.println(village.getVillageId() + "---" + village.getVillageName());
			;
		}
		return listvillage;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteVillage</p>  
	 * <p>Description:删除一条小区的记录 </p>  
	 * @author 树昌
	 * @param id 要删除的记录的ID
	 * @return
	 */
	public boolean deleteVillage(Village village) {
		
		VillageExample villageExample =new VillageExample();
		Criteria criteria = villageExample.createCriteria();
		criteria.andVillageIdEqualTo(village.getVillageId());
		village.setEnable((byte) 0);
		
		int row =villageMapper.updateByExampleSelective(village, villageExample);
		return row==1?true:false;
	}
	
	
	
	/**
	 * 
	 * <p>Title: updateVillage</p>  
	 * <p>Description:修改一条小区信息 </p>  
	 * @author 树昌
	 * @param village
	 * @return
	 */
	public boolean updateVillage(Village village) {
		
		VillageExample villageExample =new VillageExample();
		Criteria criteria = villageExample.createCriteria();
		criteria.andVillageIdEqualTo(village.getVillageId());
		village.setVillageName(village.getVillageName());
		
		int row = villageMapper.updateByExampleSelective(village, villageExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteVillageTable</p>  
	 * <p>Description:清空数据表 </p>  
	 * @author 树昌
	 * @return
	 */
	public  boolean deleteVillageTableData() {
		VillageExample villageExample = new VillageExample();
		int row = villageMapper.deleteByExample(villageExample);
		return row==1?true:false;
		
	}
}
